package com.banciyuan.bcywebview.biz.main.mineinfo.mywork;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.view.tag.TagView;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.event.EventDetailActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.List;

import de.greenrobot.daoexample.model.NativeWriteWork;
import de.greenrobot.daoexample.model.Novel;
import de.greenrobot.daoexample.model.TagDetail;
import de.greenrobot.daoexample.model.WorkMonth;

/**
 * Created by hy on 2015/5/15.
 */
public class MyNovelAdapter extends BaseAdapter {

    private List<NativeWriteWork> data;
    private Context context;
    private LayoutInflater mInflater;
    private String CHARPTER_COUNT_FORMAT;

    public MyNovelAdapter(Context context, List<NativeWriteWork> data) {
        this.context = context;
        this.data = data;
        mInflater = LayoutInflater.from(context);
        CHARPTER_COUNT_FORMAT = context.getString(R.string.charpter_count);
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int viewType = getItemViewType(position);
        NativeWriteWork nativeWriteWork = data.get(position);
        switch (viewType) {
            case 0:
                TimeViewHolder timeViewHolder;
                WorkMonth workMonth = nativeWriteWork.getWorkMonth();
                if (null == convertView) {
                    timeViewHolder = new TimeViewHolder();
                    convertView = mInflater.inflate(R.layout.writework_time_item, viewGroup, false);
                    timeViewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_mainwork_time);
                    timeViewHolder.tvCount = (TextView) convertView.findViewById(R.id.tv_mainwork_count);
                    convertView.setTag(timeViewHolder);
                } else {
                    timeViewHolder = (TimeViewHolder) convertView.getTag();
                }
                timeViewHolder.tvCount.setText(String.format(CHARPTER_COUNT_FORMAT, workMonth.getCount()));
                timeViewHolder.tvTime.setText(workMonth.getDate());
                return convertView;
            default:
                ViewHolder mViewHolder;
                if (null == convertView) {
                    mViewHolder = new ViewHolder();
                    convertView = mInflater.inflate(R.layout.mynovel_item, viewGroup, false);
                    mViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_novel_title);
                    mViewHolder.tvDesc = (TextView) convertView.findViewById(R.id.tv_novel_desc);
                    mViewHolder.seriesStatus = (TextView) convertView.findViewById(R.id.tv_series_status);
                    mViewHolder.tags = (TagView) convertView.findViewById(R.id.tags);
                    convertView.setTag(mViewHolder);
                } else {
                    mViewHolder = (ViewHolder) convertView.getTag();
                }
                final Novel novel = nativeWriteWork.getNovel();
                if (!TextUtils.isEmpty(novel.getTitle())) {
                    mViewHolder.tvTitle.setText(Html.fromHtml(novel.getTitle()));
                }
                String intro = novel.getIntro();
                if (TextUtils.isEmpty(intro)) {
                    mViewHolder.tvDesc.setVisibility(View.GONE);
                } else {
                    mViewHolder.tvDesc.setVisibility(View.VISIBLE);
                    mViewHolder.tvDesc.setText(Html.fromHtml(novel.getIntro()));
                }
                processTagData(mViewHolder, novel, 0);
                if (novel.isPost_in_set()) {
                    mViewHolder.seriesStatus.setVisibility(View.VISIBLE);
                    if (novel.isSet_end()) {
                        mViewHolder.seriesStatus.setText(context.getResources().getString(R.string.series_done));
                        mViewHolder.seriesStatus.setBackgroundResource(R.drawable.shape_series_orange);
                    } else {
                        mViewHolder.seriesStatus.setText(context.getResources().getString(R.string.series_ing));
                        mViewHolder.seriesStatus.setBackgroundResource(R.drawable.shape_series_green);
                    }
                } else {
                    mViewHolder.seriesStatus.setVisibility(View.GONE);
                }
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DetailDataHelper.gotoWriterDetail(context, novel.getRp_id(), novel.getWp_id(), false);
                    }
                });
                return convertView;
        }
    }

    private void processTagData(ViewHolder viewHolder, final Novel novel, final int from) {
        if (novel.getPost_tags() != null && novel.getPost_tags().size() > 0) {
            viewHolder.tags.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tags.setVisibility(View.GONE);
        }
        if (novel.getPost_tags() != null && novel.getPost_tags().size() > 0) {
            viewHolder.tags.setTlist(novel.getPost_tags());
            viewHolder.tags.setTagViewClick(new TagView.OnTagClickListener() {
                @Override
                public void onClick(TagDetail t) {
                    if (!TextUtils.isEmpty(t.getWork())) {
                        if (!TextUtils.isEmpty(t.getWid())) {
                            Intent intent = new Intent(context, CircleSmoothActivity.class);
                            intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getWork());
                            intent.putExtra(gotoUtil.INTENT_VALUE_TWO, t.getWid());
                            intent.putExtra("from", from);
                            context.startActivity(intent);
                        }
                    } else if (!TextUtils.isEmpty(t.getEvent_id())) {
                        gotoUtil.gotoAct(context, EventDetailActivity.class, t.getEvent_id());
                    } else {
                        Intent intent = new Intent(context, CircleTagSmoothActivity.class);
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, t.getTag_name());
                        intent.putExtra("type", "tag");
                        intent.putExtra("from", from);
                        context.startActivity(intent);
                    }
                }
            });
        }
    }

    public void setData(List<NativeWriteWork> novels) {
        this.data = novels;
    }

    static class TimeViewHolder {
        TextView tvTime;
        TextView tvCount;
    }

    static class ViewHolder {
        TextView tvTitle;
        TextView tvDesc;
        TextView seriesStatus;
        TagView tags;
    }

    @Override
    public int getItemViewType(int position) {
        NativeWriteWork nativeWriteWork = data.get(position);
        if (0 == nativeWriteWork.getType()) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }
}
